<%@ taglib prefix="cs" uri="futuretense_cs/ftcs1_0.tld" %>
<%@ taglib prefix="ics" uri="futuretense_cs/ics.tld" %>
<%@ taglib prefix="render" uri="futuretense_cs/render.tld" %>
<%@ taglib prefix="asset" uri="futuretense_cs/asset.tld" %>
<%@ taglib prefix="assetset" uri="futuretense_cs/assetset.tld" %>
<%@ taglib prefix="listobject" uri="futuretense_cs/listobject.tld" %>
<%//
// * INPUT:
//      - c: Asset Type
//      - cid: Asset ID
//		- immediateonly:  Boolean value indicating whether to scatter inherited attribute values or not. A false value indicates that inherited values should be scattered (the default). 
// * INPUT OBJECT:
//			- hmAttributes (HashMap)
// * OUTPUT OBJECT:
//			- hmAttributes (HashMap)
//%>
<%@ page import="COM.FutureTense.Interfaces.IList" %>
<%@ page import="COM.FutureTense.Interfaces.Utilities" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.util.Locale" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<cs:ftcs>
<ics:if condition='<%=ics.GetVar("seid")!=null%>'>
<ics:then>
	<render:logdep cid='<%=ics.GetVar("seid")%>'   c="SiteEntry"/>
</ics:then>
</ics:if>
<ics:if condition='<%=ics.GetVar("eid")!=null%>'>
<ics:then>
	<render:logdep cid='<%=ics.GetVar("eid")%>'   c="CSElement"/>
</ics:then>
</ics:if>
<%
try {
	// Input parameters
	String assetType = ics.GetVar("c");
	String assetId = ics.GetVar("cid");
	String immediateonly = ics.GetVar("immediateonly");
	HashMap hmAttributes = (HashMap) ics.GetObj("hmAttributes");
	if ((assetType == null) || (assetId == null) || (hmAttributes == null))
	   throw new Exception("LOGIC_GetAttributeValues - Null input parameters");

	//Attribute Table should be calculated base on the Asset Type
	String attrType = "";
	if ("Page".equals(assetType)){
		attrType="PageAttribute";
	}else if (assetType.indexOf("_CD")!=-1){
		attrType=assetType.replace("_CD", "_A");
	}else if (assetType.indexOf("_PD")!=-1){
		attrType=assetType.replace("_PD", "_A");
	}else if (assetType.indexOf("_P")!=-1){
		attrType=assetType.replace("_P", "_A");
	}else{
		attrType=assetType.replace("_C", "_A");
	}
	//AssetSet to load the flex asset	
	%><assetset:setasset name="contenido" type='<%=assetType%>' id='<%=assetId%>'/><%

 	//Dates formatting
   SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   dateParser.setLenient(true);
   SimpleDateFormat dateFormatter = null;
	//TODO - Date format base on input parameter
   dateFormatter = new SimpleDateFormat("MMMMM d',' yyyy", new Locale("en", "GB"));
   %><listobject:create name="listaAtributos" columns="attributetypename,attributename,direction"/><%
	Iterator it = hmAttributes.entrySet().iterator();
	while (it.hasNext()) {
		Map.Entry e = (Map.Entry)it.next();
		String nombreAtributo = (String)e.getKey();
		String tipoAtributo = (String)e.getValue();
		if (tipoAtributo.indexOf("R")==-1){
			//Related Assets (asset named associations) are not flexattributes.
			%><listobject:addrow name="listaAtributos"><%
			   %><listobject:argument name="attributetypename" value="<%=attrType%>"/><%
			   %><listobject:argument name="attributename" value="<%=nombreAtributo%>"/><%
			   %><listobject:argument name="direction" value="none"/><%
			%></listobject:addrow><%
		}
	}
	%><listobject:tolist name="listaAtributos" listvarname="listaSalidaAtributos"/><%
 	%><assetset:getmultiplevalues name="contenido" prefix="contenido" list="listaSalidaAtributos" immediateonly="<%=immediateonly%>" byasset="false"/><%
	// Iterate to fill the HashMap with the corresponding values
	it = hmAttributes.entrySet().iterator();
	while (it.hasNext()) {
		Map.Entry e = (Map.Entry)it.next();
		String nAtt= (String)e.getKey(); //attribute name
		String vAtt= (String)e.getValue(); //attribute value
		IList listaAtributo = ics.GetList("contenido:"+nAtt);
		if (vAtt.indexOf("R")!=-1){
			%><asset:children type='<%=assetType%>' assetid='<%=assetId%>' list="assetList" code='<%=nAtt%>' order="nrank"/><%
			IList assetList = ics.GetList("assetList");
			if (assetList != null && assetList.hasData()) {
				String auxString="";
				if (vAtt.indexOf("M")!=-1){
					Vector valorAtributo = new Vector();
					int numAssets = assetList.numRows();
					for (int i = 1; i<=numAssets;i++) {
						assetList.moveTo(i);
						auxString=assetList.getValue("oid")+"#"+assetList.getValue("otype");
						valorAtributo.add(auxString);
					}
					hmAttributes.put(nAtt,valorAtributo);
				}else{
					auxString=assetList.getValue("oid")+"#"+assetList.getValue("otype");
					hmAttributes.put(nAtt,auxString);
				}
				ics.RegisterList("assetList",null); 
			}else if (vAtt.indexOf("M")!=-1){
				//Return an empty vector
				hmAttributes.put(nAtt,new Vector());
			}
		}else if( listaAtributo!=null && listaAtributo.hasData() ){
			listaAtributo.moveTo(1);
			if (vAtt.equals("D")) {
			  String value = "";
			  try {
				Date date = dateParser.parse(listaAtributo.getValue("value"));
				value = dateFormatter.format(date);
			  } catch (Exception ex) {
				value = listaAtributo.getValue("value");
			  }
			  // To avoid return null
			  if (value!=null){
				hmAttributes.put(nAtt,value);
			  }else{
				  hmAttributes.put(nAtt,"");
			  }
			  	/* List */
			} else if (vAtt.equals("L")) {
				hmAttributes.put(nAtt,listaAtributo);
				/* Text */
			}else if (vAtt.equals("T")) {
			  //If the type attribute is Text it is no possible to use the tag assetset:getmultiplevalues
				%><assetset:getattributevalues name="contenido" attribute="<%=nAtt%>" typename="<%=attrType%>" immediateonly="<%=immediateonly%>" listvarname="listaText" /><%
			  IList listaText = ics.GetList("listaText");
			  if (listaText!=null && listaText.hasData()) {
				listaText.moveTo(1);
				if (listaText.getValue("value")!=null){
					// No blank space at the end
					String sText= listaText.getValue("value");
					if (sText.endsWith(" ")){
						sText = sText.substring(0,sText.length()-1);
					}
					hmAttributes.put(nAtt,sText);
				}else{
					hmAttributes.put(nAtt,"");
				}
			  }
			  ics.RegisterList("listaText",null); 
				// If it is multievaluated
			}else if (vAtt.indexOf("M") != -1){
				Vector valorAtributo = new Vector();
				for (int j=0;j<listaAtributo.numRows();j++){
					listaAtributo.moveTo(j+1);
					if (vAtt.indexOf("D")!=-1) {
						String value = "";
						try {
						   java.util.Date date = dateParser.parse(listaAtributo.getValue("value"));
						   value = dateFormatter.format(date);
						} catch (Exception ex) {
						 value = listaAtributo.getValue("value");
						}
						valorAtributo.add(value);
					}else {
						String auxString = (listaAtributo.getValue("value")).trim();
						if(!auxString.equals("")){
							valorAtributo.add(auxString);
						}
					}
				} //for
				  hmAttributes.put(nAtt,valorAtributo);
			}else {	//Default input value -- No value
				// No blank space at the end
				String sText= listaAtributo.getValue("value");
				if (sText.endsWith(" ")){
					sText = sText.substring(0,sText.length()-1);
				}
				hmAttributes.put(nAtt,sText);
			}
			ics.RegisterList("contenido:"+nAtt,null); 
		} // if hasData
		else {
			if (vAtt.indexOf("M") != -1){
				//Return an empty vector
				hmAttributes.put(nAtt,new Vector());
			}else if (vAtt.equals("L")){
				//Return an empty Ilist
				IList emptyList=null;
				hmAttributes.put(nAtt,emptyList);
			}
			else{
				//Return an empty string
				hmAttributes.put(nAtt,"");
			}
		}
	} // for of the attributes

} catch(Exception e) {
// Error element
}

%></cs:ftcs>